/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.mp.common.api.controller;

import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.config.WxConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * jsapi 签名
 * @author yami
 */
@RestController
@RequestMapping("/p/wx/jsapi")
@Api(tags = "微信jsapi接口")
@AllArgsConstructor
public class JsapiController {

    private final WxConfig wxConfig;

    @PostMapping("/createJsapiSignature")
    @ApiOperation(value = "生成jsapi签名", notes = "根据url生成当前页面的jsapi签名")
    @ApiImplicitParam(name = "url", value = "url", required = true, dataType = "String")
    public ServerResponseEntity<WxJsapiSignature> createJsapiSignature(@RequestBody String url) {
        WxJsapiSignature jsapiSignature;
        try {
            jsapiSignature = wxConfig.getWxMpService().createJsapiSignature(url);
        } catch (WxErrorException e) {
            throw new YamiShopBindException(e.getError().toString());
        }
        return ServerResponseEntity.success(jsapiSignature);
    }
}
